local shenshi = fk.CreateSkill{
    name = "wk_heg__shenshi",
}

Fk:loadTranslationTable{
    ["wk_heg__shenshi"] = "审时",
    [":wk_heg__shenshi"] = "其他角色获得你的牌后，你可令其摸一张牌，然后此回合结束时，若此回合内所有以此法摸牌的角色于以此法摸牌后未对与你势力相同的角色造成过伤害，与你势力相同的角色各摸一张牌。",

    ["$wk_heg__shenshi1"] = "深中足智，鉴时审情。",
    ["$wk_heg__shenshi2"] = "数语之言，审时度势。",
}

local H = require "packages.ol_hegemony.util"

shenshi:addEffect(fk.AfterCardsMove,{
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    local room = player.room
    if not player:hasSkill(shenshi.name) then return false end
    for _, move in ipairs(data) do
      if move.from and move.from == player and move.to and move.to ~= player and move.toArea == Card.PlayerHand then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
              event:setCostData(self, { to = move.to })
              -- return not H.compareKingdomWith(move.from, move.to)
              return true
          end
        end
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).to
    to:drawCards(1, shenshi.name)
    room:setPlayerMark(to, "wk_heg__shenshi_draw-turn", 1)
  end,
})

shenshi:addEffect(fk.DetermineDamageCaused,{
  can_refresh = function(self, event, target, player, data)
    return data.from and data.from:getMark("wk_heg__shenshi_draw-turn") > 0 and H.compareKingdomWith(data.to, player)
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "wk_heg__shenshi_damage-turn", 1)
  end,
})

shenshi:addEffect(fk.TurnEnd,{
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return player:usedSkillTimes(shenshi.name, Player.HistoryTurn) > 0 and player:getMark("wk_heg__shenshi_damage-turn") == 0
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if player:getMark("wk_heg__shenshi_damage-turn") == 0 then
    local targets = table.filter(room.alive_players, function(p) return H.compareKingdomWith(p, player) end)
    room:sortByAction(targets)
    for _, p in ipairs(targets) do
        if not p.dead then
          p:drawCards(1, shenshi.name)
        end
      end
    end
  end,
})

return shenshi